import TurndownService from 'turndown'

export function convertHtmlToMarkdown(htmlContent) {
  const turndownService = new TurndownService()
  turndownService.addRule('input', {
    //@ts-ignore
    [Symbol.iterator]() {
      //@ts-ignore
      return undefined
    },
    anchor(name) {
      return ''
    },
    big() {
      return ''
    },
    blink() {
      return ''
    },
    bold() {
      return ''
    },
    charAt(pos) {
      return ''
    },
    charCodeAt(index) {
      return 0
    },
    codePointAt(pos) {
      return undefined
    },
    concat(strings) {
      return ''
    },
    endsWith(searchString, endPosition) {
      return false
    },
    fixed() {
      return ''
    },
    fontcolor(color) {
      return ''
    },
    includes(searchString, position) {
      return false
    },
    indexOf(searchString, position) {
      return 0
    },
    italics() {
      return ''
    },
    lastIndexOf(searchString, position) {
      return 0
    },
    length: 0,
    link(url) {
      return ''
    },
    repeat(count) {
      return ''
    },
    slice(start, end) {
      return ''
    },
    small() {
      return ''
    },
    startsWith(searchString, position) {
      return false
    },
    strike() {
      return ''
    },
    sub() {
      return ''
    },
    substr(from, length) {
      return ''
    },
    substring(start, end) {
      return ''
    },
    sup() {
      return ''
    },
    toLocaleLowerCase(locales) {
      return ''
    },
    toLocaleUpperCase(locales) {
      return ''
    },
    toLowerCase() {
      return ''
    },
    toString() {
      return ''
    },
    toUpperCase() {
      return ''
    },
    trim() {
      return ''
    },
    valueOf() {
      return ''
    },
    fontsize(size) {
      return ''
    },
    localeCompare(that, locales, options) {
      return 0
    },
    match(matcher) {
      //@ts-ignore
      return undefined
    },
    normalize(form) {
      return ''
    },
    replace(searchValue, replacer) {
      return ''
    },
    search(regexp) {
      return 0
    },
    split(separator, limit) {
      return []
    },
    filter: ['input'],
    //@ts-ignore
    replacement: function (content, node) {
      return node.getAttribute('checked') === null ? '[ ] ' : '[x] '
    }
  })
  turndownService.addRule('codeBlock', {
    filter: ['pre'],
    //@ts-ignore
    replacement: function (content, node) {
      const element = node
      return '```' + element.getAttribute('code-lang') + '\n' + node.textContent + '```\n'
    }
  })
  turndownService.keep(['del', 'u'])
  const markdown = turndownService.turndown(htmlContent)
  // const title = pageTitle?.trim() || UNTITLED_PAGE_NAME
  // FileExporter.exportTextFile(title + '.md', markdown, 'text/plain')
  return markdown
}
